#include "s_para.h"
#include <stdio.h>
#include <string.h>
#include "s_nor_flash.h"

#define S_AUTO_PLANT_US_COMPLEMENT_VALUE			((unsigned int) ~0)

typedef struct
{
	unsigned int run_time_sec;
	unsigned int run_time_sec_complement;
	unsigned int halt_time_sec;
	unsigned char led_enable;
	unsigned char buzzer_enable;
	unsigned int day_start_time_sec;
	unsigned int day_end_time_sec;
	unsigned int motor_pwm;
	unsigned int day_alarm_time_sec;
}s_para_t;

static s_para_t s_para;

signed char s_para_check(void)
{
	if(s_para.run_time_sec + s_para.run_time_sec_complement != S_AUTO_PLANT_US_COMPLEMENT_VALUE)
	{
		printf("not comlpement\n");
		return (-1);
	}
	return 0;
}



signed char s_para_set_run_time_sec(unsigned int data)
{
	s_para.run_time_sec = data;
	s_para.run_time_sec_complement = ~(data);
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_run_time_sec(void)
{
	return s_para.run_time_sec;
}

signed char s_para_set_halt_time_sec(unsigned int data)
{
	s_para.halt_time_sec = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_halt_time_sec(void)
{
	return s_para.halt_time_sec;
}

signed char s_para_set_led_enable(unsigned char data)
{
	s_para.led_enable = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}
unsigned char s_para_get_led_enable(void)
{
	return s_para.led_enable;
}

signed char s_para_set_buzzer_enable(unsigned char data)
{
	s_para.buzzer_enable = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}
unsigned char s_para_get_buzzer_enable(void)
{
	return s_para.buzzer_enable;
}

signed char s_para_set_day_start_time_sec(unsigned int data)
{
	s_para.day_start_time_sec = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_day_start_time_sec(void)
{
	return s_para.day_start_time_sec;
}

signed char s_para_set_day_end_time_sec(unsigned int data)
{
	s_para.day_end_time_sec = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_day_end_time_sec(void)
{
	return s_para.day_end_time_sec;
}

signed char s_para_set_motor_pwm(unsigned int data)
{
	s_para.motor_pwm = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_motor_pwm(void)
{
	return s_para.motor_pwm;
}

signed char s_para_set_day_alarm_time_sec(unsigned int data)
{
	s_para.day_alarm_time_sec = data;
	S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	return 0;
}

unsigned int s_para_get_day_alarm_time_sec(void)
{
	return s_para.day_alarm_time_sec;
}

signed char s_para_init(void)
{
	S_Nor_Flash_Read_Para_Struct(&s_para, sizeof(s_para_t));
	if(s_para_check()) {
		s_para_set_run_time_sec(600);
		s_para_set_halt_time_sec(1800);
		s_para_set_led_enable(0);
		s_para_set_buzzer_enable(0);
		s_para_set_day_start_time_sec(0);
		s_para_set_day_end_time_sec(0);
		s_para_set_motor_pwm(100);
		s_para_set_day_alarm_time_sec(0);
		// S_Nor_Flash_Write_Para_Struct(&s_para, sizeof(s_para_t));
	} else {
		// s_para_set_run_time_sec(600);
		// s_para_set_halt_time_sec(1800);
		// s_para_set_led_enable(0);
		// s_para_set_buzzer_enable(0);
		// s_para_set_day_start_time_sec(0);
		// s_para_set_day_end_time_sec(0);
		// s_para_set_motor_pwm(100);
		// s_para_set_day_alarm_time_sec(0);
	}
	return 0;
}
